//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "Eonia.h"
using namespace Cephei::QL::Indexes::Ibor;
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Indexes/IborIndex.h>
#include <gen/QL/Currency.h>
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Times/Period.h>
#include <gen/QL/Indexes/OvernightIndex.h>
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL::Indexes;
using namespace Cephei::QL;
using namespace Cephei::QL::Times;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Indexes::Ibor::CEonia::CEonia (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>^ h) : COvernightIndex(CEonia::typeid)
{
    CYieldTermStructure^ _Ch;
    try
    {
#ifdef HANDLE
        _phEonia = NULL;
#endif
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>::IsSome::get (h))
        {
            _Ch = safe_cast<CYieldTermStructure^> (h->Value);
            _Ch->Lock();
        }
        Handle<QuantLib::YieldTermStructure>& _h = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>::IsSome::get (h) ? static_cast<Handle<QuantLib::YieldTermStructure>&> (_Ch->GetHandle ()) : Handle<QuantLib::YieldTermStructure>()); //1
        _ppEonia = new boost::shared_ptr<QuantLib::Eonia> (new QuantLib::Eonia ( _h ));
        SetOvernightIndex (boost::dynamic_pointer_cast<QuantLib::OvernightIndex> (*_ppEonia));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ch != nullptr) _Ch->Unlock();
    }
}
Cephei::QL::Indexes::Ibor::CEonia::CEonia (boost::shared_ptr<QuantLib::Eonia>& childNative, Object^ owner) : COvernightIndex(CEonia::typeid)
{
#ifdef HANDLE
	_phEonia = NULL;
#endif
	_ppEonia = &childNative;
    _ppOvernightIndex = new boost::shared_ptr<QuantLib::OvernightIndex> (boost::dynamic_pointer_cast<QuantLib::OvernightIndex> (*_ppEonia));
}
Cephei::QL::Indexes::Ibor::CEonia::CEonia (QuantLib::Eonia& childNative, Object^ owner) : COvernightIndex(CEonia::typeid)
{
#ifdef HANDLE
	_phEonia = NULL;
#endif
	_ppEonia = new boost::shared_ptr<QuantLib::Eonia> (&childNative);
    _ppOvernightIndex = new boost::shared_ptr<QuantLib::OvernightIndex> (boost::dynamic_pointer_cast<QuantLib::OvernightIndex> (*_ppEonia));
    _EoniaOwner = owner;
    _OvernightIndexOwner = owner;
}

Cephei::QL::Indexes::Ibor::CEonia::CEonia (CEonia^ copy) : COvernightIndex(CEonia::typeid)
{
#ifdef HANDLE
	_phEonia = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppEonia = new boost::shared_ptr<QuantLib::Eonia> (copy->GetShared());
        _ppOvernightIndex = new boost::shared_ptr<QuantLib::OvernightIndex> (boost::dynamic_pointer_cast<QuantLib::OvernightIndex> (*_ppEonia));
    }
}
Cephei::QL::Indexes::Ibor::CEonia::CEonia (System::Type^ t) : COvernightIndex(CEonia::typeid)
{
#ifdef HANDLE
	_phEonia = NULL;
#endif
	if (!t->IsSubclassOf(CEonia::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Indexes::Ibor::CEonia::CEonia (QuantLib::Handle<QuantLib::Eonia>& childNative, Object^ owner)  : COvernightIndex(CEonia::typeid)
{
	_phEonia = &childNative;
	_ppEonia = &static_cast<boost::shared_ptr<QuantLib::Eonia>>(childNative.currentLink());
    _ppOvernightIndex = new boost::shared_ptr<QuantLib::OvernightIndex> (boost::dynamic_pointer_cast<QuantLib::OvernightIndex> (*_ppEonia));
    _EoniaOwner = owner;
}
Cephei::QL::Indexes::Ibor::CEonia::CEonia (QuantLib::Handle<QuantLib::Eonia> childNative)  : COvernightIndex(CEonia::typeid)
{
	_phEonia = &childNative;
	_ppEonia = &static_cast<boost::shared_ptr<QuantLib::Eonia>>(childNative.currentLink());
    _ppOvernightIndex = new boost::shared_ptr<QuantLib::OvernightIndex> (boost::dynamic_pointer_cast<QuantLib::OvernightIndex> (*_ppEonia));
}
#endif
#ifdef STRUCT
Cephei::QL::Indexes::Ibor::CEonia::CEonia (QuantLib::Eonia childNative)  : COvernightIndex(CEonia::typeid)
{
#ifdef HANDLE
	_phEonia = NULL;
#endif
	_ppEonia = new boost::shared_ptr<QuantLib::Eonia> (new QuantLib::Eonia (childNative));
    _ppOvernightIndex = new boost::shared_ptr<QuantLib::OvernightIndex> (boost::dynamic_pointer_cast<QuantLib::OvernightIndex> (*_ppEonia));
}
#endif

Cephei::QL::Indexes::Ibor::CEonia::~CEonia ()
{
    if (_ppEonia != NULL)
    {
	    delete _ppEonia;
        _ppEonia = NULL;
    }
}
Cephei::QL::Indexes::Ibor::CEonia::!CEonia ()
{
    if (_ppEonia != NULL)
    {
	    delete _ppEonia;
    }
}
QuantLib::Eonia& Cephei::QL::Indexes::Ibor::CEonia::GetReference ()
{
    if (_ppEonia == NULL) throw gcnew NativeNullException ();
	return **_ppEonia;
}
boost::shared_ptr<QuantLib::Eonia>& Cephei::QL::Indexes::Ibor::CEonia::GetShared ()
{
    if (_ppEonia == NULL) throw gcnew NativeNullException ();
	return *_ppEonia;
}
QuantLib::Eonia* Cephei::QL::Indexes::Ibor::CEonia::GetPointer ()
{
    if (_ppEonia == NULL) throw gcnew NativeNullException ();
	return &**_ppEonia;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::Eonia>& Cephei::QL::Indexes::Ibor::CEonia::GetHandle ()
{
	if (_phEonia == NULL)
	{
		_phEonia = new Handle<QuantLib::Eonia> (*_ppEonia);
	}
	return *_phEonia;
}
#endif
bool Cephei::QL::Indexes::Ibor::CEonia::HasNative () 
{
	return (_ppEonia != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Indexes::Ibor::IEonia^ Cephei::QL::Indexes::Ibor::CEonia_Factory::Create (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>^ h)
{
    return gcnew CEonia ( h);
}
